package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.Banner;
import com.dachuang.canjiren.entity.User;
import com.dachuang.canjiren.entity.Volunteer;
import com.dachuang.canjiren.entity.Work;
import com.dachuang.canjiren.mapper.UserMapper;
import com.dachuang.canjiren.mapper.WorkMapper;
import com.dachuang.canjiren.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public R insertWork(Integer userId, String mechanism, String name, String demand, String mail, String introduce, String phone, String matter, String wages, Integer state) {
        if (Objects.isNull(userId)){
            return R.error(BizCodeEnume.ERROR20006);
        }
        if (StringUtils.isEmpty(mechanism)){
            return R.error(BizCodeEnume.ERROR70001);
        }
        if (StringUtils.isEmpty(name)){
            return R.error(BizCodeEnume.ERROR50001);
        }
        if (StringUtils.isEmpty(demand)){
            return R.error(BizCodeEnume.ERROR70002);
        }
        if (StringUtils.isEmpty(mail)){
            return R.error(BizCodeEnume.ERROR70003);
        }
        if (StringUtils.isEmpty(introduce)){
            return R.error(BizCodeEnume.ERROR70004);
        }
        if (StringUtils.isEmpty(phone)){
            return R.error(BizCodeEnume.ERROR70006);
        }
        if (StringUtils.isEmpty(matter) ){
            return R.error(BizCodeEnume.ERROR70007);
        }
        if (StringUtils.isEmpty(wages)){
            return R.error(BizCodeEnume.ERROR70008);
        }

        //验证userId
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)){
            return R.error(BizCodeEnume.ERROR20004);
        }

        Work work = new Work();
        work.setId(userId);
        work.setMechanism(mechanism);
        work.setName(name);
        work.setDemand(demand);
        work.setMail(mail);
        work.setIntroduce(introduce);
        work.setPhone(phone);
        work.setMatter(matter);
        work.setWages(wages);
        work.setState(state);


        if (workMapper.insertWork(work) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectWork() {
        List<Work> list = workMapper.selectWork();
        Map<String,Object> map = new HashMap<>();
        if (list.size() > 0){
            map.put("data",list);
            return R.ok(map);
        }
        return R.error();
    }

}
